# coding:utf-8
# @Time    : 2021/11/29 17:30
# @Author  : Martin
from page_object.base.base_page import BasePage
from page_object.welcome_page import WelcomePage


class LoginPage(BasePage):

    _base_url = "https://xxxxxx/ids/login?service=/nspas-web-in" \
                                 "/auth?targetUrl=/nspas-web-in/welcome.htm"

    def __init__(self, driver, logger):
        self.driver = driver
        self.logger = logger
        self.open(self._base_url)
        # 如果已登录，就退出
        if self.driver.current_url == "/nspas-web-in/welcome.htm":
            wp = WelcomePage(self.driver, self.logger)
            wp.log_out()

    def log_in(self, user):
        self.input('id', 'username', user)
        # 等待输入完成,2个输入框之间输入太快 会导致登录失败
        # self.wait(0.5)
        self.input('id', 'password-hide', user)
        # self.wait(0.5)
        self.click('xpath', '//a[@onclick="submit_form()"]')
        self.logger.info(f'{user}登录成功')
        return WelcomePage(self.driver, self.logger)
